import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String title;
  final bool showBackButton;
  final List<Widget>? rightButtons;
  final VoidCallback? onBackPressed;
  final PreferredSizeWidget? bottom;
  final bool loading;
  final bool loadingRotating;
  final VoidCallback? onLoadingTap;
  final Widget? centerWidget;
  final Color? backgroundColor;
  const CustomAppBar({
    Key? key,
    required this.title,
    this.showBackButton = true,
    this.rightButtons,
    this.onBackPressed,
    this.bottom,
    this.loading = false,
    this.loadingRotating = true,
    this.onLoadingTap,
    this.centerWidget,
    this.backgroundColor,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: backgroundColor ?? Colors.transparent,
      elevation: 0,
      automaticallyImplyLeading: false,
      iconTheme: const IconThemeData(color: Colors.white),
      centerTitle: false,
      title: Padding(
          padding:
              EdgeInsets.only(left: (showBackButton || !loading) ? 0 : 16.w),
          child: Stack(
            children: [
              Positioned(
                left: 0,
                top: 0,
                bottom: 0,
                child: showBackButton
                    ? IconButton(
                        padding: EdgeInsets.zero,
                        icon: Image.asset(
                          'assets/images/Group 1321318317@2x.png',
                          width: 20.w,
                          height: 20.w,
                        ),
                        onPressed: onBackPressed ?? () => Get.back(),
                      )
                    : SizedBox.shrink(),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  centerWidget != null
                      ? centerWidget!
                      : Text(
                          title,
                          style: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.w500,
                            fontSize: 16.sp,
                          ),
                        ),
                  if (loading) ...[
                    const SizedBox(width: 8),
                    GestureDetector(
                      onTap: onLoadingTap,
                      child: SizedBox(
                        width: 24,
                        height: 24,
                        child: loadingRotating
                            ? const LoadingIcon()
                            : const Icon(
                                Icons.refresh,
                                color: Colors.white,
                                size: 20,
                              ),
                      ),
                    ),
                  ],
                ],
              ),
              Positioned(
                right: 0,
                bottom: 0,
                child: rightButtons != null
                    ? Row(children: rightButtons!)
                    : SizedBox.shrink(),
              )
            ],
          )),
      titleSpacing: 0,
      bottom: bottom,
    );
  }

  @override
  Size get preferredSize =>
      Size.fromHeight(kToolbarHeight + (bottom?.preferredSize.height ?? 0));
}

// 创建白色圆角按钮的辅助方法
class WhiteButton extends StatelessWidget {
  final String text;
  final VoidCallback onPressed;
  final double height;
  final double width;

  const WhiteButton({
    Key? key,
    required this.text,
    required this.onPressed,
    this.height = 26,
    this.width = 48,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 6),
      child: SizedBox(
        height: height,
        width: width,
        child: ElevatedButton(
          onPressed: onPressed,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.white,
            foregroundColor: Colors.black54,
            elevation: 0,
            padding: const EdgeInsets.symmetric(horizontal: 4),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(14),
              side: const BorderSide(color: Colors.black12),
            ),
          ),
          child: Text(
            text,
            style: const TextStyle(fontSize: 13),
          ),
        ),
      ),
    );
  }
}

// 创建白色圆角按钮的辅助方法
class BlueButton extends StatelessWidget {
  final String text;
  final VoidCallback onPressed;

  const BlueButton({
    super.key,
    required this.text,
    required this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 30.h,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 2),
        child: ElevatedButton(
          onPressed: onPressed,
          style: ElevatedButton.styleFrom(
            backgroundColor: Color(0xFF4CAF50),
            foregroundColor: Colors.black,
            elevation: 0,
            padding: const EdgeInsets.symmetric(horizontal: 4),
          ),
          child: Text(
            text,
            style: const TextStyle(color: Colors.white, fontSize: 12),
          ),
        ),
      ),
    );
  }
}

// 创建一个新的有状态组件来处理动画
class LoadingIcon extends StatefulWidget {
  const LoadingIcon({super.key});

  @override
  State<LoadingIcon> createState() => _LoadingIconState();
}

class _LoadingIconState extends State<LoadingIcon>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    )..repeat(); // 使动画持续重复
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: _controller,
      child: const Icon(
        Icons.refresh,
        color: Colors.white,
        size: 20,
      ),
    );
  }
}
